//
//  StatusCell.swift
//  SinaWeiBo20
//
//  Created by tarena on 16/3/25.
//  Copyright © 2016年 tarena. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    var bottomViewTopConstraints: Constraint?
    //定义微博模型
    var status: Status? {
        didSet{
            //给顶部视图 设置模型数据
            topView.status = status
            
            //卸掉顶部约束
            bottomViewTopConstraints?.uninstall()
            
            //需要根据树模型 提供的数据 判断是否显示转发微博视图
            if let retweetedStatus = status?.retweeted_status {
                
                //设置转发微博的数据源
                retweetedView.retweetedStatus = retweetedStatus
                //1.修改约束
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                //显示转发微博
                retweetedView.hidden = false
            }else{
                //
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomViewTopConstraints = make.top.equalTo(topView.snp_bottom).constraint
                })
                
                //隐藏 转发微博视图
                retweetedView.hidden  = true
            }
            
        }
    }
    
    //cell 入口
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .None
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //自定义cell
    private func setupUI() {
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        
        //设置约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(topView.snp_left)
            make.right.equalTo(topView.snp_right)
            make.top.equalTo(topView.snp_bottom)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            self.bottomViewTopConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            //很重要的一个约束
            make.bottom.equalTo(bottomView.snp_bottom)
        }
        
    }
    
    
    // 懒加载子视图
    //顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    
    //转发微博
    private lazy var retweetedView: StatusRetweetedView = StatusRetweetedView()
    
    //底部视图
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
    

}
